using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class LIST_LOOKUPDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procLIST_LOOKUP_add]";
        private string _strSPUpdateName = "dbo.[procLIST_LOOKUP_update]";
        private string _strSPDeleteName = "dbo.[procLIST_LOOKUP_delete]";
        private string _strSPGetName = "dbo.[procLIST_LOOKUP_get]";
        private string _strSPGetName_getcategory = "dbo.[procLIST_LOOKUP_getbycate]";
        string _strSPGetName_getcategoryview = "procLIST_LOOKUP_getcateview";
        private string _strSPGetAllName = "dbo.[procLIST_LOOKUP_getall]";
        private string _strSPGetPages = "dbo.[procLIST_LOOKUP_getpaged]";
        private string _strSPIsExist = "dbo.[procLIST_LOOKUP_isexist]";
        private string _strTableName = "[LIST_LOOKUP]";
        private string _strSPGetTransferOutName = "dbo.[procLIST_LOOKUP_gettransferout]";
        string _strSPGetCountName = "procLIST_LOOKUP_getcount";
        string _strSPGetByIndexName = "procLIST_LOOKUP_getindex";
        #endregion Local Variable

        #region Method
        public LIST_LOOKUPInfo Get(
        String DTB,
        String LOOKUP_CATEGORY,
        String CODE,
        ref string sErr)
        {
            LIST_LOOKUPInfo objEntr = new LIST_LOOKUPInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), LOOKUP_CATEGORY);
            AddParameter(LIST_LOOKUPInfo.Field.CODE.ToString(), CODE);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = (LIST_LOOKUPInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable Get_Category(
                String DTB,
                String LOOKUP_CATEGORY,

                ref string sErr)
        {
            LIST_LOOKUPInfo objEntr = new LIST_LOOKUPInfo();
            InitConnect();
            InitSPCommand(_strSPGetName_getcategory);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), LOOKUP_CATEGORY);
            
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            //if (list.Rows.Count > 0)
            //    objEntr = (LIST_LOOKUPInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            LIST_LOOKUPInfo result = new LIST_LOOKUPInfo();
            result.DTB = (dt.Rows[i][LIST_LOOKUPInfo.Field.DTB.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.DTB.ToString()]));
            result.LOOKUP_CATEGORY = (dt.Rows[i][LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString()]));
            result.CODE = (dt.Rows[i][LIST_LOOKUPInfo.Field.CODE.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.CODE.ToString()]));
            result.LOOKUP_ALT = (dt.Rows[i][LIST_LOOKUPInfo.Field.LOOKUP_ALT.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.LOOKUP_ALT.ToString()]));
            result.DESCRIPTN = (dt.Rows[i][LIST_LOOKUPInfo.Field.DESCRIPTN.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.DESCRIPTN.ToString()]));
            result.DESCRIPTN1 = (dt.Rows[i][LIST_LOOKUPInfo.Field.DESCRIPTN1.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.DESCRIPTN1.ToString()]));
            result.STATUS = (dt.Rows[i][LIST_LOOKUPInfo.Field.STATUS.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.STATUS.ToString()]));
            result.CREATEDBY = (dt.Rows[i][LIST_LOOKUPInfo.Field.CREATEDBY.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.CREATEDBY.ToString()]));
            result.MODIFIEDBY = (dt.Rows[i][LIST_LOOKUPInfo.Field.MODIFIEDBY.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_LOOKUPInfo.Field.MODIFIEDBY.ToString()]));
            result.CREATEDDATE = (dt.Rows[i][LIST_LOOKUPInfo.Field.CREATEDDATE.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][LIST_LOOKUPInfo.Field.CREATEDDATE.ToString()]));
            result.MODIFIEDDATE = (dt.Rows[i][LIST_LOOKUPInfo.Field.MODIFIEDDATE.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][LIST_LOOKUPInfo.Field.MODIFIEDDATE.ToString()]));

            return result;
        }

        public DataTable GetAll(
        String DTB,
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), DTB);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(LIST_LOOKUPInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), objEntr.LOOKUP_CATEGORY);
            AddParameter(LIST_LOOKUPInfo.Field.CODE.ToString(), objEntr.CODE);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_ALT.ToString(), objEntr.LOOKUP_ALT);
            AddParameter(LIST_LOOKUPInfo.Field.DESCRIPTN.ToString(), objEntr.DESCRIPTN);
            AddParameter(LIST_LOOKUPInfo.Field.DESCRIPTN1.ToString(), objEntr.DESCRIPTN1);
            AddParameter(LIST_LOOKUPInfo.Field.STATUS.ToString(), objEntr.STATUS);
            AddParameter(LIST_LOOKUPInfo.Field.CREATEDBY.ToString(), objEntr.CREATEDBY);
            AddParameter(LIST_LOOKUPInfo.Field.MODIFIEDBY.ToString(), objEntr.MODIFIEDBY);
            AddParameter(LIST_LOOKUPInfo.Field.CREATEDDATE.ToString(), objEntr.CREATEDDATE);
            AddParameter(LIST_LOOKUPInfo.Field.MODIFIEDDATE.ToString(), objEntr.MODIFIEDDATE);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(LIST_LOOKUPInfo objEntr)
        {
            openConnection();
            InitSPCommand(_strSPUpdateName);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), objEntr.LOOKUP_CATEGORY);
            AddParameter(LIST_LOOKUPInfo.Field.CODE.ToString(), objEntr.CODE);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_ALT.ToString(), objEntr.LOOKUP_ALT);
            AddParameter(LIST_LOOKUPInfo.Field.DESCRIPTN.ToString(), objEntr.DESCRIPTN);
            AddParameter(LIST_LOOKUPInfo.Field.DESCRIPTN1.ToString(), objEntr.DESCRIPTN1);
            AddParameter(LIST_LOOKUPInfo.Field.STATUS.ToString(), objEntr.STATUS);
            AddParameter(LIST_LOOKUPInfo.Field.CREATEDBY.ToString(), objEntr.CREATEDBY);
            AddParameter(LIST_LOOKUPInfo.Field.MODIFIEDBY.ToString(), objEntr.MODIFIEDBY);
            AddParameter(LIST_LOOKUPInfo.Field.CREATEDDATE.ToString(), objEntr.CREATEDDATE);
            AddParameter(LIST_LOOKUPInfo.Field.MODIFIEDDATE.ToString(), objEntr.MODIFIEDDATE);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String DTB,
        String LOOKUP_CATEGORY,
        String CODE
        )
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), LOOKUP_CATEGORY);
            AddParameter(LIST_LOOKUPInfo.Field.CODE.ToString(), CODE);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

     

        public Boolean IsExist(
        String DTB,
        String LOOKUP_CATEGORY,
        String CODE
        )
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), LOOKUP_CATEGORY);
            AddParameter(LIST_LOOKUPInfo.Field.CODE.ToString(), CODE);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(LIST_LOOKUPInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public int GetCountRecord(string db, string cate, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPGetCountName);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), db);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), cate);
            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public LIST_LOOKUPInfo GetByPos(string db, string cate, int pos, ref string sErr)
        {
            DTO.LIST_LOOKUPInfo objEntr = new LIST_LOOKUPInfo();
            InitConnect();
            InitSPCommand(_strSPGetByIndexName);
            AddParameter("INX", pos);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), db);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), cate);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (list.Rows.Count > 0)
                objEntr = (LIST_LOOKUPInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable Get_Category_View(
               String DTB,
               String LOOKUP_CATEGORY,

               ref string sErr)
        {
            LIST_LOOKUPInfo objEntr = new LIST_LOOKUPInfo();
            InitConnect();
            InitSPCommand(_strSPGetName_getcategoryview);
            AddParameter(LIST_LOOKUPInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_LOOKUPInfo.Field.LOOKUP_CATEGORY.ToString(), LOOKUP_CATEGORY);


            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            //if (list.Rows.Count > 0)
            //    objEntr = (LIST_LOOKUPInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
